//
//  PayPasswordViewController.m
//  PigMarket
//
//  Created by mac on 2019/4/30.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PayPasswordViewController.h"
#import "PayPasswordView.h"
#import "ConfirmPasswordViewController.h"
#import "AccountSecurityViewController.h"
#import "PhoneViewController.h"


@interface PayPasswordViewController ()

/**
 passwordView
 */
@property (nonatomic , strong) PayPasswordView *passwordView;

@property (nonatomic, copy) NSString *orginpassword; //原始密码值
@property (nonatomic, copy) NSString *newpassword; //新支付密码
@property (nonatomic, copy) NSString *againNewpassword; //再次输入的密码

@end

@implementation PayPasswordViewController

#pragma mark - view life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.baseNavView.line.hidden = YES;

    [self setUpView];
}


#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    [self.view addSubview:self.passwordView];
}


- (void) initLayout {
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
}


- (void) initAction {
    [self.passwordView.confirmBtn addTarget:self action:@selector(nextTapped) forControlEvents:UIControlEventTouchUpInside];
}


- (void)initData {
    
    switch (self.type) {
        case PayPasswordViewControllerType_SetUp:
        {
            self.passwordView.titleLab.text = @"请输入支付密码";
            self.passwordView.forgetPasswordBtn.hidden = YES;
            [self.passwordView.confirmBtn setTitle:@"下一步" forState:UIControlStateNormal];
            
        }
            break;
            
        case PayPasswordViewControllerType_AgainSetUp:
        {
            self.passwordView.titleLab.text = @"再次输入支付密码";
            self.passwordView.forgetPasswordBtn.hidden = YES;
            [self.passwordView.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
            
        }
            break;

        case PayPasswordViewControllerType_OriginPay:
            {
                self.passwordView.titleLab.text = @"请输入原支付密码";
                self.passwordView.forgetPasswordBtn.hidden = NO;
                [self.passwordView.confirmBtn setTitle:@"下一步" forState:UIControlStateNormal];

            }
            break;
        
        case PayPasswordViewControllerType_NewPay:
            {
                self.passwordView.titleLab.text = @"请输入新支付密码";
                self.passwordView.forgetPasswordBtn.hidden = YES;
                [self.passwordView.confirmBtn setTitle:@"下一步" forState:UIControlStateNormal];

            }
            break;
            
        default:
            {
                self.passwordView.titleLab.text = @"再次输入新支付密码";
                self.passwordView.forgetPasswordBtn.hidden = YES;
                [self.passwordView.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
            }
            break;
    }
}

#pragma mark - delegate

#pragma mark - target action

- (void)nextTapped{
    [self resignResponder];
    NSString *payPwd = self.passwordView.passwordView.vertificationCode;

    if (payPwd.length != 6) {
        [self toastWithContent:@"请输入六位支付密码"];
        return;
    }
    
    PayPasswordViewController *vc = [[PayPasswordViewController alloc] init];
    
    switch (self.type) {
        case PayPasswordViewControllerType_SetUp:
        {
            vc.type = PayPasswordViewControllerType_AgainSetUp;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case PayPasswordViewControllerType_OriginPay:
            {
                vc.type = PayPasswordViewControllerType_NewPay;
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
         case PayPasswordViewControllerType_NewPay:
            {
                vc.type = PayPasswordViewControllerType_AgainNewPay;
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        default:
            {
                for (UIViewController *vc in self.navigationController.viewControllers) {
                    if ([vc isKindOfClass:[AccountSecurityViewController class]]) {
                        [self.navigationController popToViewController:vc animated:YES];
                    }
                }
            }
            break;
    }
    
}

#pragma mark - private method
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignResponder];
}


- (void)resignResponder {
    
    for (UITextField *tf in self.passwordView.passwordView.textFieldArray) {
        [tf resignFirstResponder];
    }
}


#pragma mark - getter and setter

- (PayPasswordView *)passwordView {
    
    if (!_passwordView) {
        _passwordView = [[PayPasswordView alloc] init];
        _passwordView.backgroundColor = [UIColor whiteColor];
        
        @weakify(self);
        _passwordView.dealForgetPasswordBlock = ^{
            @strongify(self);
            PhoneViewController *phoneVC = [[PhoneViewController alloc] init];
            phoneVC.tipsTitle = ModifyPassword;
            phoneVC.isPay = YES;
            [self.navigationController pushViewController:phoneVC animated:YES];        };
    }
    return _passwordView;
}



@end
